<?php



defined('BASEPATH') OR exit('No direct script access allowed');

include 'linkdatabase.php';
include 'findInDatabase.php';


class SearchBook extends CI_Controller {
  public function index(){
		$type=$_GET['type'];
		$name=$_GET['name'];
    $status="确认完成";
		$data=array();
    $item=array('title','author','fprice','price','date','image','sellername','contect','contectId');

    $link = linkdatabase::link();
    @mysqli_select_db($link, "book");

		if($type==1){
      $sql="SELECT * FROM sellbook WHERE `status`='$status' AND `title` LIKE '%{$name}%'";
		  $result = mysqli_query($link, $sql);
		  $num = mysqli_num_rows($result);
			for($i=0; $i<$num;$i++){
				$row= mysqli_fetch_assoc($result);
			  $name=findInDatabase::findname($row['uid']);
				$item['title']=$row['title'];
				$item['author']=$row['author'];
				$item['fprice']=$row['origin'];
				$item['price']=$row['present'];
				$item['date']=$row['date'];
				$item['image']=$row['image'];
				$item['sellername']=$name['name'];
				$item['contect']=$name['contact'];
				$item['contectId']=$name['contactId'];
				$data[$i]=$item;
			}

			header(	'Content-Type:text/json');
			echo json_encode($data);
			
		}else{
      $sql="SELECT * FROM buybook WHERE `status`='$status' AND `title` LIKE '%{$name}%'";
		  $result = mysqli_query($link, $sql);
		  $num = mysqli_num_rows($result);
			for($i=0; $i<$num;$i++){
				$row= mysqli_fetch_assoc($result);
			  $name=findInDatabase::findname($row['uid']);
				$item['title']=$row['title'];
				$item['author']=$row['author'];
				$item['date']=$row['date'];
				$item['buyername']=$name['name'];
				$item['contect']=$name['contact'];
				$item['contectId']=$name['contactId'];
				$data[$i]=$item;
			}
			header(	'Content-Type:text/json');
			echo json_encode($data);
		}


			

  }
}